<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Spinatiev Ivan
 * Date: 10/12/12
 * Time: 5:19 PM
 * To change this template use File | Settings | File Templates.
 */

class Page_Be_Products_Poems extends Page
{
	/**
	 * @var Db_Products_Poems
	 */
	protected $tableObject;

	function __construct() {
		$this->idTableObject = get('id', 'int');
		$this->actionsTableObject = array(
			'addEditAction',
			'saveAction',
		);
		$this->classTableObject = 'Db_Products_Poems';
		parent::__construct();
	}

	public function defaultAction() {
		if (perm('POEMS_EDIT')) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
	    }

		self::$TPL->appendJsFile('admin/poems');
		self::$TPL->setBodyTemplate('list_poems');
		return array(
			'type' => 'display'
		);
	}

	public function addEditAction() {
		perm('POEMS_EDIT', 'poems');
		if (!empty($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' &quot;' . t( T_POEMS_TITLE , $this->tableObject->objectArray->pe_title) . '&quot;', '?action=addEdit&id=' . $this->tableObject->objectArray->id, true);
			self::$TPL->assign('poem', $this->tableObject->objectArray);
		} else {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'));
		}

		$projectIdentifiers = Db_Projects::getDistinctProjects();
		self::$TPL->assign('projectIdentifiers', $projectIdentifiers);
		self::$TPL->setBodyTemplate('add_edit_poems');
		return array(
			'type' => 'display'
		);
	}

	public function saveAction() {
		perm('POEMS_EDIT', 'poems');
		$this->message = t(T_FORM_MSG, 'success');
		$poemName = strtolower(post('pe_title', 'string'));
		if (!empty($poemName)) {
			$this->tableObject->pe_title = $poemName;
			$this->tableObject->pe_content = strtolower(post('pe_content', 'string'));
			$this->tableObject->pe_author = strtolower(post('pe_author', 'string'));
			$this->tableObject->pe_show_on_page = post('pe_show_on_page', 'int');
			$this->tableObject->pe_project_identifier = post('pe_project_identifier', 'str');
			if(!$this->tableObject->save()) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		} else {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'field_name_is_empty');
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function deleteAction() {
		perm('POEMS_EDIT', 'poems');
		$this->message = t(T_FORM_MSG, 'success');
		if (!Db_Products_Poems::abstractDelete($this->idTableObject)) {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function ajaxLoadPoemsDataAction() {
		$allColumns = array('pe_title', 'pe_content', 'pe_author', 'pe_show_on_page', 'action');
		$searchColumns = array('pe_title', 'pe_content', 'pe_author');
		list($searchQuery,$orderBy, $limit) = Utils::getSqlQueriesForPagination($allColumns, $searchColumns, 'Db_Products_Poems', 'getTotalPoems');
		$poemsList = Db_Products_Poems::getAllPoems($limit, $searchQuery, $orderBy);
		self::$TPL->assign('poemsList', $poemsList);
		return array(
			'type' => 'encoded_json',
			'content' =>self::$TPL->display('json_list_poems', true)
		);
	}
}
